<?php
	class Rights
	{
		const TYPE_ROLE = 2;
		const TYPE_ACTION = 1;
		public $Guest = "guest";
		
		public function getRoles($userid = NULL)
		{
			$flag = $userid == NULL ? true : false;
			$userroles = array();
			$db = WebApp::app()->db;
			if($userid == NULL)
			{
				$userroles = $db->select(array("*"),array('maskitem'),'name = "'.$this->Guest.'"');
			}
			else
			{
				$userid = WebApp::app()->db->select(array('id'), array("users"), "username = '". $userid ."' OR email = '" . $userid . "'");
				$userroles = $db->select(array("*"),array('maskassignment'),'user_id = '.$userid[0]->id);
			}
			$masks = array();
			$tmp;
			foreach($userroles as $role)
			{
				if($flag)
				{
				$tmp = $db->select(array('*'),array('maskitemchild'), 'parent = "'.$role->name.'"');
				}
				else
				{
				$tmp = $db->select(array('*'),array('maskitemchild'), 'parent = "'.$role->itemname.'"');
				}
				foreach($tmp as $t)
				{
					$masks[strtolower($t->child)] = strtolower($t->child);
				}
			}
			return $masks;
		}
	}
?>